﻿using System.Windows.Automation;
using QuickUIT.Utilities;

namespace QuickUIT.Controls
{
    /// <summary>
    /// Represents a progress bar control. 
    /// </summary>
    public class UIProgressBar : UIBaseControl
    {
        /// <summary>
        /// Gets whether the current range is read only. 
        /// </summary>
        public new bool IsRangeReadOnly
        {
            get { return base.IsRangeReadOnly; }
        }

        /// <summary>
        /// Gets the current range value for the progress bar control
        /// </summary>
        public new double RangeValue
        {
            get { return base.RangeValue; }
        }

        /// <summary>
        /// Gets the maximum value for the progress bar control. 
        /// </summary>
        public new double Maximum
        {
            get { return base.Maximum; }
        }

        /// <summary>
        /// Gets the minimum value for the progress bar control.
        /// </summary>
        public new double Minimum
        {
            get { return base.Minimum; }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="UIProgressBar"/> class. 
        /// </summary>
        public UIProgressBar()
            : base(ControlType.ProgressBar)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="UIProgressBar"/> class.
        /// </summary>
        /// <param name="parent">The parent of this control.</param>
        public UIProgressBar(UIBaseControl parent)
            : base(parent, ControlType.ProgressBar)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="UIProgressBar"/> class.
        /// </summary>
        /// <param name="locator">The locator used to find the specific control.</param>
        public UIProgressBar(string locator)
            : base(null, ControlType.ProgressBar, locator)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="UIProgressBar"/> class.
        /// </summary>
        /// <param name="parent">The parent of this control.</param>
        /// <param name="locator">The locator used to find the specific control.</param>
        public UIProgressBar(UIBaseControl parent, string locator)
            : base(parent, ControlType.ProgressBar, locator)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="UIProgressBar"/> class.
        /// </summary>
        /// <param name="control">The underlying <see cref="AutomationElement"/> of the control.</param>
        public UIProgressBar(AutomationElement control) : base(control, ControlType.ProgressBar)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="UIProgressBar"/> class.
        /// </summary>
        /// <param name="parent">The parent of the control</param>
        /// <param name="control">The underlying <see cref="AutomationElement"/> of the control.</param>
        public UIProgressBar(UIBaseControl parent, AutomationElement control) : base(parent, control, ControlType.ProgressBar)
        {
        }

        /// <summary>
        /// Waits for the progress bar to reach completion, or until the timeout value is reached.
        /// </summary>
        /// <param name="milliseconds">The maximum time in milliseconds to wait for the progress bar to finish.</param>
        /// <returns>True if the progress bar has reached completion, false if timeout value is reached first.</returns>
        public bool WaitForCompletion(int milliseconds)
        {
            ValidationUtility.ValidateValueGreaterThanOrEqualTo(milliseconds, 0, "milliseconds");
            var timer = new AutomationTimer(milliseconds);
            timer.Start();
            while (!timer.HasTimedOut())
            {
                if (RangeValue >= Maximum)
                {
                    return true; 
                }
            }
            return false;
        }
    }
}
